package com.longcai.piaojia.conn;

import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.http.note.HttpInlet;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/7.
 */
@HttpInlet(Conn.QUESTION_CATEGORY)
public class ProblemsListGet extends BaseAsyGet<List<ProblemsListGet.ProblemEntity>> {

    public ProblemsListGet(AsyCallBack<List<ProblemEntity>> asyCallBack) {
        super(asyCallBack);
    }

    @Override
    protected List<ProblemEntity> parser(JSONObject object) throws Exception {

        TOAST = object.optString("message");

        if (object.optInt("code") == 200) {


            JSONArray array = object.optJSONArray("data");

            List<ProblemEntity> entityList = new ArrayList<>();

            for (int i = 0; i < array.length(); i++) {

                ProblemEntity problemEntity = new ProblemEntity();

                JSONObject entityObject = array.optJSONObject(i);

                problemEntity.title = entityObject.optString("title");
                problemEntity.state = entityObject.optString("state");
                problemEntity.id = entityObject.optString("id");

                JSONArray arrayAnswer = entityObject.optJSONArray("list");

                for (int j = 0; j <arrayAnswer.length(); j++) {

                    ProblemItemEntity problemItemEntity = new ProblemItemEntity();

                    JSONObject ansObject = arrayAnswer.optJSONObject(j);

                    problemItemEntity.ask = ansObject.optString("ask");
                    problemItemEntity.answer = ansObject.optString("answer");
                    problemItemEntity.id = ansObject.optString("id");
                    problemItemEntity.title = ansObject.optString("title");

                    problemEntity.problemItemEntities.add(problemItemEntity);

                }


                entityList.add(problemEntity);

            }


            return entityList;
        }

        return null;
    }

    public static class ProblemEntity {


        public String title, state, id;

        public List<ProblemItemEntity> problemItemEntities = new ArrayList<>();

    }


    public static class ProblemItemEntity {

        public String id, ask, answer, title, state, typeid, orderid;

    }

}
